1 using UnityEngine;
2 using
Hashtable = ExitGames.Client.Photon.Hashtable;
3
4 [RequireComponent(
typeof(PhotonView))]
5 public
class CubeLerp : Photon.MonoBehaviour
6 {
7     
private Vector3 latestCorrectPos;
8     
private Vector3 onUpdatePos;
9     
private float fraction;
10
11
12     
public void Awake()
13     {
14         
if (photonView.isMine)
15         {
16             
this.enabled = false; // due to this, Update() is not called on the owner client.
17         }
18
19         latestCorrectPos = transform.position;
20         onUpdatePos = transform.position;
21     }

22
23     ///
<summary>
24     ///
While script is observed (in a PhotonView), this is called by PUN with a stream to write or read.
25     ///
</summary>
26     ///
<remarks>
27     ///
The property stream.isWriting is true for the owner of a PhotonView. This is the only client that
28     ///
should write into the stream. Others will receive the content written by the owner and can read it.
29     ///

30     ///
Note: Send only what you actually want to consume/use, too!
31     ///
Note: If the owner doesn't write something into the stream, PUN won't send anything.
32     ///
</remarks>
33     ///
<param name="stream">Read or write stream to pass state of this GameObject (or whatever else).</param>
34     ///
<param name="info">Some info about the sender of this stream, who is the owner of this PhotonView (and GameObject).</param>
35     
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
36     {
37         
if (stream.isWriting)
38         {
39             Vector3 pos = transform.localPosition;
40             Quaternion rot = transform.localRotation;
41             stream.Serialize(
ref pos);
42             stream.Serialize(
ref rot);
43         }
44         
else
45         {
46             
// Receive latest state information
47             Vector3 pos = Vector3.zero;
48             Quaternion rot = Quaternion.identity;
49
50             stream.Serialize(
ref pos);
51             stream.Serialize(
ref rot);
52
53             latestCorrectPos = pos;
// save this to move towards it in FixedUpdate()
54             onUpdatePos = transform.localPosition;
// we interpolate from here to latestCorrectPos
55             fraction =
0; // reset the fraction we alreay moved. see Update()
56
57             transform.localRotation = rot;
// this sample doesn't smooth rotation
58         }
59     }
60
61     
public void Update()
62     {
63         
// We get 10 updates per sec. sometimes a few less or one or two more, depending on variation of lag.
64         
// Due to that we want to reach the correct position in a little over 100ms. This way, we usually avoid a stop.
65         
// Lerp() gets a fraction value between 0 and 1. This is how far we went from A to B.
66         
//
67         
// Our fraction variable would reach 1 in 100ms if we multiply deltaTime by 10.
68         
// We want it to take a bit longer, so we multiply with 9 instead.
69
70         fraction = fraction + Time.deltaTime *
9;
71         transform.localPosition = Vector3.Lerp(onUpdatePos, latestCorrectPos, fraction);
// set our pos between A and B
72     }
73 }


this.enabled = false; due to this, Update() is not called on the owner client.

While script is observed (in a PhotonView), this is called by PUN with a stream to write or read.

The property stream.isWriting is true for the owner of a PhotonView. This is the only client that

should write into the stream. Others will receive the content written by the owner and can read it.

Note: Send only what you actually want to consumeuse, too!

Note: If the owner doesn't write something into the stream, PUN won't send anything.

Read or write stream to pass state of this GameObject (or whatever else).

Some info about the sender of this stream, who is the owner of this PhotonView (and GameObject).

Receive latest state information

latestCorrectPos = pos; save this to move towards it in FixedUpdate()

onUpdatePos = transform.localPosition; we interpolate from here to latestCorrectPos

fraction = 0; reset the fraction we alreay moved. see Update()

transform.localRotation = rot; this sample doesn't smooth rotation

We get 10 updates per sec. sometimes a few less or one or two more, depending on variation of lag.

Due to that we want to reach the correct position in a little over 100ms. This way, we usually avoid a stop.

Lerp() gets a fraction value between 0 and 1. This is how far we went from A to B.

Our fraction variable would reach 1 in 100ms if we multiply deltaTime by 10.

We want it to take a bit longer, so we multiply with 9 instead.

transform.localPosition = Vector3.Lerp(onUpdatePos, latestCorrectPos, fraction); set our pos between A and B




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.562 lượt xem

Gõ tìm kiếm nhanh...